/**
 * Copyright (c) 2022 Beijing Jiaotong University
 * PhotLab is licensed under [Open Source License].
 * You can use this software according to the terms and conditions of the [Open Source License].
 * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *
 * See the [Open Source License] for more details.
 *
 * Author: Zhenjie Wei
 * Created: Oct. 24, 2023
 * Supported by: National Key Research and Development Program of China
 */

import LogicCore from '@/common/logic/core'
import FrameLayer from './layers/frame'
import ScalarLayer from './layers/scalar'
import MeshLayer from './layers/mesh'
import SelectLayer from './layers/select'
import LinkLayer from './layers/link'
import ToastLayer from './layers/toast'
import MoveObjectLayer from './layers/move'
import ResizeObjectLayer from './layers/resize'
import TextArea from './objects/text'
import CompLayer from './layers/comp'
import IRenderable from '@/common/logic/mixins/renderable'
import { IObject } from '@/common/logic/handlers/object'
import { graphManager } from '@/services/phot_client/graph'
import RenderNode from './models/node'

export default class Designer {
    private _core: LogicCore
    private _stage: HTMLCanvasElement

    private _frameLayer: FrameLayer
    private _scalarLayer: ScalarLayer
    private _meshLayer: MeshLayer
    private _selectLayer: SelectLayer
    private _moveLayer: MoveObjectLayer
    private _resizeLayer: ResizeObjectLayer
    private _linkLayer: LinkLayer
    private _compLayer: CompLayer
    private _toastLayer: ToastLayer

    public get core() {
        return this._core
    }

    constructor(stage: HTMLCanvasElement) {
        const core = new LogicCore()
        this._core = core
        this._stage = stage

        this._frameLayer = new FrameLayer('frame', 2)
        this._scalarLayer = new ScalarLayer('scalar', 4)
        this._meshLayer = new MeshLayer('mesh', -1)
        this._selectLayer = new SelectLayer('select', 1)
        this._moveLayer = new MoveObjectLayer('move', 3)
        this._resizeLayer = new ResizeObjectLayer('resize', 3)
        this._linkLayer = new LinkLayer('link', -1)
        this._compLayer = new CompLayer('comp', 0)
        this._toastLayer = new ToastLayer('toast', 5)

        this._setupScene()
        this._registerBaseEvents()

        this._core.focus()
    }

    private _setupScene() {
        const core = this._core

        core.connect(this._stage)

        core.mount(this._frameLayer)
        core.mount(this._scalarLayer)
        core.mount(this._meshLayer)
        core.mount(this._selectLayer)
        core.mount(this._moveLayer)
        core.mount(this._resizeLayer)
        core.mount(this._compLayer)
        core.mount(this._linkLayer)
        core.mount(this._toastLayer)

        core.attach(graphManager)
    }

    private _registerBaseEvents() {
        this._core.on('keydown.tab', () => {
            // this._core.switchMemoryToNext()
        })
    }

    public new() {
        this._core.switchMemory(this._core.createMemory())
    }
}
